package com.hopu.controller;

import com.hopu.domain.ExtCproductC;
import com.hopu.domain.Factoryc;
import com.hopu.service.ExtCproductCService;
import com.hopu.service.FactorycService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/extCproductC")
public class ExtCproductCController {

    @Autowired
    private ExtCproductCService extCoproductCService;
    @Autowired
    private FactorycService factorycService;

    /**
     * 到修改页面
     *
     *
     * @param model
     * @return
     */
    @RequestMapping("/toupdate")
    public String toupdate(String extCproductId, Model model,HttpSession session) {
        ExtCproductC extCproductC = extCoproductCService.selectByPrimaryKey(extCproductId);
        model.addAttribute("obj", extCproductC);
        //厂家名称
        List<Factoryc> all = factorycService.findAll();
        model.addAttribute("dataList",all);
        model.addAttribute("contractProductId",extCproductC.getContractProductId());
        return "/basicinfo/contract/jExtCproductUpdate";
    }



    /**
     * 修改方法
     *
     * @param
     * @return
     */
    @RequestMapping("/update")
    public String update(ExtCproductC extCproductC,HttpSession session, Model model ,String contractProductId) {
        extCproductC.setAmount(extCproductC.getPrice().multiply(extCproductC.getCnumber()));
        extCoproductCService.updateByPrimaryKey(extCproductC);
        if (contractProductId==null){
            String productId = (String) session.getAttribute("contractProductId");
            contractProductId=productId;
        }
        model.addAttribute("contractProductId",contractProductId);
        return "redirect:toCreate";
    }

    /**
     * 删除一条数据
     *
     * @param
     * @return
     */
    @RequestMapping("/deleteById")
    public String deleteById(String[] extCproductId,String contractProductId,HttpSession session,Model model) {
        if (extCproductId != null ) {
            for (String fid : extCproductId) {
                extCoproductCService.deleteByPrimaryKey(fid);
            }
        }
        if (contractProductId==null){
            String productId = (String) session.getAttribute("contractProductId");
            model.addAttribute("contractProductId",productId);
            return "redirect:toCreate";
        }
        return "redirect:toCreate";
    }




    /**
     * 查询全部
     *
     * @param model
     * @return
     */



    @RequestMapping("/toCreate")
    public String toCreate(String contractProductId, Model model , HttpSession session){

        List<Factoryc> all = factorycService.findAll();
        model.addAttribute("dataList",all);

        List<ExtCproductC> extCproductCS = extCoproductCService.findAll(contractProductId);
        model.addAttribute("extCproductCS",extCproductCS);

        model.addAttribute("contractProductId",contractProductId);
        if (contractProductId==null){
            String productId = (String) session.getAttribute("contractProductId");
            System.out.println("productId = 我是第二次" + productId);
            model.addAttribute("contractProductId",productId);

            List<ExtCproductC> extCproductCS2 = extCoproductCService.findAll(productId);
            model.addAttribute("extCproductCS",extCproductCS2);
        }else {
            session.removeAttribute("contractProductId");
        }

        return "/basicinfo/contract/jExtCproductCreate";
    }



    @RequestMapping("/insert")
    public String insert(ExtCproductC extCproductC, String contractProductId, HttpSession session) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        extCproductC.setExtCproductId(uuid);

        extCproductC.setContractProductId(contractProductId);

        extCproductC.setAmount(extCproductC.getPrice().multiply(extCproductC.getCnumber()));
        extCoproductCService.insert(extCproductC);

        session.setAttribute("contractProductId",contractProductId);
        return "redirect:toCreate";
    }


}
